package com.integratedapps.test;

import com.integratedapps.ns.test.data.external.salesorder.WsSalesOrder;
import com.integratedapps.ns.test.data.external.salesorder.WsSalesOrderLine;
import com.integratedapps.ns.test.services.operations.filesparser.WsFilesParserService;
import com.integratedapps.ns.test.services.operations.filesparser.WsParseFile;
import com.integratedapps.ns.test.services.operations.filesparser.WsParseFileResponse;
import csvfilereader.CSVFileReader;
import java.math.BigInteger;
import java.util.List;
import javax.jws.WebService;
import salesorder.SalesOrder;
import salesorder.SalesOrderLine;

@WebService(
    serviceName = "filesParserService",
    portName = "port",
    endpointInterface = "com.integratedapps.ns.test.services."
        + "operations.filesparser.WsFilesParserService",
    targetNamespace = "http://integrated-apps.com/ns/test/services/"
        + "operations/files-parser",
    wsdlLocation = "WEB-INF/wsdl/filesParserService/"
        + "svn.integrated-apps.com/infra/test-tasks/files-parser/xml/service-1.00.wsdl")
public class FilesParserService implements WsFilesParserService {

    public WsParseFileResponse parseFile(
            final WsParseFile body) {
        
        
        WsParseFileResponse wsResponse = new WsParseFileResponse();
        wsResponse.setSalesOrders(new WsParseFileResponse.SalesOrders());
        
        List<SalesOrder> salesOrdersList = CSVFileReader.parseFile(body.getFile());

        for(SalesOrder so: salesOrdersList){
            WsSalesOrder wsSalesOrder = new WsSalesOrder();
            wsSalesOrder.setNumber(so.getSalesOrderNumber());
            wsSalesOrder.setPurchaseOrder(so.getPurchaseOrderNumber());
            wsSalesOrder.setLines(new WsSalesOrder.Lines());
            for(SalesOrderLine sol: so.getLines()) {
                WsSalesOrderLine wsSalesOrderLine = new WsSalesOrderLine ();
                wsSalesOrderLine.setItem(new WsSalesOrderLine.Item());
                wsSalesOrderLine.getItem().setPartNumber(sol.getPartNumber());
                wsSalesOrderLine.getItem().setDisplayName(sol.getDisplayName());
                wsSalesOrderLine.getItem().setQuantity(BigInteger.valueOf(sol.getQuantity()));
                
                WsSalesOrderLine.Item.SerialNumbers serialNumbers = new WsSalesOrderLine.Item.SerialNumbers();
                serialNumbers.getValues().addAll(sol.getSerialNumbers());
              
                wsSalesOrderLine.getItem().setSerialNumbers(serialNumbers);
                wsSalesOrderLine.setShipping(new WsSalesOrderLine.Shipping());
                wsSalesOrderLine.getShipping().setGrossWeight(BigInteger.valueOf(sol.getGrossWeight()));
                wsSalesOrderLine.getShipping().setShipmentId(sol.getShipmentId());
                
                WsSalesOrderLine.Shipping.BoxNumbers boxNumbers = new WsSalesOrderLine.Shipping.BoxNumbers();
                boxNumbers.getValues().addAll(sol.getBoxNumbers());
                
                wsSalesOrderLine.getShipping().setBoxNumbers(boxNumbers);
                wsSalesOrderLine.setBilling(new WsSalesOrderLine.Billing());
                wsSalesOrderLine.getBilling().setInvoiceId(sol.getInvoiceId());
                
                wsSalesOrder.getLines().getValues().add(wsSalesOrderLine);

            }
            
            wsResponse.getSalesOrders().getValues().add(wsSalesOrder);
        }
        return wsResponse;

        //throw new UnsupportedOperationException("Not implemented yet.");
    }
    
}
